package com.fjt.book_pay.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fjt.book_pay.dao.IOrderDao;
import com.fjt.book_pay.pojo.Bank;
import com.fjt.book_pay.pojo.BookPay;
import com.fjt.book_pay.pojo.Order;
import com.fjt.book_pay.pojo.P_Record;
import com.fjt.book_pay.pojo.P_User;
import com.fjt.book_pay.service.P_UserService;

@Controller
public class P_UserController {
	
	private P_UserService p_UserService;
	private IOrderDao orderDao;
	private Order order;
	
	public Order getOrder() {
		return order;
	}
	@Resource(name="order")
	public void setOrder(Order order) {
		this.order = order;
	}
	
	public IOrderDao getOrderDao() {
		return orderDao;
	}
	@Resource
	public void setOrderDao(IOrderDao orderDao) {
		this.orderDao = orderDao;
	}
	public P_UserService getUserService() {
		return p_UserService;
	}
	@Resource(name="p_userService")
	public void setUserService(P_UserService userService) {
		this.p_UserService = userService;
	}
	
	@RequestMapping("/p_user/mailcheck") 
    @ResponseBody
    public ModelAndView mailcheck(@RequestBody P_User user,HttpServletRequest request,HttpServletResponse response){
    	Map<String,Object> modelMap=new HashMap<String,Object>();
    	ModelAndView modelView=new ModelAndView(); 
		System.out.println("/p_user/mailcheck");
		modelMap=p_UserService.sendmailcode(user, request);
    	modelView.addAllObjects(modelMap);  
    	System.out.println(request.getSession().getAttribute("code"));
    	return modelView;
		
    }
	
    @RequestMapping("/p_user/register") 
    @ResponseBody
    public ModelAndView register(@RequestBody P_User user,HttpServletRequest request){
    	Map<String,Object> modelMap=new HashMap<String,Object>();
    	ModelAndView modelView=new ModelAndView(); 
		System.out.println("/p_user/register");
//          modelMap=userService.validateUser(username, password, request); 
//		Enumeration enum1 = request.getHeaderNames();  
//	      
//	    while(enum1.hasMoreElements()){  
//	        String key = (String)enum1.nextElement();  
//	        String value = request.getHeader(key);  
//	        System.out.println(key + ":" + value);  
//	    }  
	    
		HttpSession session=request.getSession(false);
		System.out.println(session.getId());
		int code=(Integer) session.getAttribute("code");
		
		System.out.println(code);
		if(user.getEmail_check()==code){
			modelMap=p_UserService.addP_User(user);
	    	modelView.addAllObjects(modelMap);  
	    	return modelView;
		}else{
			modelMap.put("code", 0);
    		modelMap.put("result", "验证码错误！");
    		modelView.addAllObjects(modelMap);
    		System.out.println("/p_user/register code error");
    		return modelView;
		}
    }
    
    @RequestMapping("/p_user/login") 
    @ResponseBody
    public ModelAndView login(@RequestBody P_User user,
    		HttpServletRequest request){  
    	Map<String,Object> modelMap=new HashMap<String,Object>();
    	
    	ModelAndView modelView=new ModelAndView(); 
    		modelMap=p_UserService.validateUser(user, request); 
        	modelView.addAllObjects(modelMap); 
        	System.out.println("/p_user/login");
        	return modelView;
    	
    }
    
    @RequestMapping("/p_user/logout") 
    @ResponseBody
    public ModelAndView logout(HttpServletRequest request){  
    	Map<String,Object> modelMap=new HashMap<String,Object>();
    	HttpSession session=request.getSession(false);
    	if(session!=null){
    		session.removeAttribute("username");
    		session.invalidate();
    	}
    	
    	ModelAndView modelView=new ModelAndView();
    	modelMap.put("code", 1);
    	modelMap.put("result", "注销成功");
    	modelView.addAllObjects(modelMap); 
    	System.out.println("/p_user/logout");
    	return modelView;
    	
    }
    
    @RequestMapping("/p_user/userinfo") 
    @ResponseBody
    public ModelAndView getUserInfo(@RequestBody P_User user
    		,HttpServletRequest request){
    	Map<String,Object> modelMap=new HashMap<String,Object>();
    	
    	ModelAndView modelView=new ModelAndView(); 
    	HttpSession session=request.getSession();
    	String username=(String)session.getAttribute("username");
    	if(username!=null){
    		System.out.println(username);
    		user.setPay_name(username);
    		modelMap=p_UserService.getUserInfo(user); 
    		Bank bank=new Bank();
    		bank.setBank_number(p_UserService.getUserDao().findUserBank(user));
    		int bank_money=p_UserService.getBankService().getBankDao().checkmoney(bank);
    		modelMap.put("bank_money",bank_money);
        	modelView.addAllObjects(modelMap);  
        	System.out.println("/p_user/userinfo"+username);
        	return modelView;
    	}else{
    		modelMap.put("code", 0);
    		modelMap.put("result", "登录失效，请重新登录！");
    		modelView.addAllObjects(modelMap);
    		System.out.println("/p_user/userinfo no session");
    		return modelView;
    	}
    	
    	
    }
    
    @RequestMapping("/p_user/reset") 
    @ResponseBody
    public ModelAndView resetpassword(@RequestBody P_User user,HttpServletRequest request){  
    	Map<String,Object> modelMap=new HashMap<String,Object>();
    	ModelAndView modelView=new ModelAndView(); 
    	HttpSession session=request.getSession(false);
    	System.out.println(session.getId());
		int code=(Integer) session.getAttribute("code");
		System.out.println(code);
		if(user.getEmail_check()==code){
			modelMap=p_UserService.resetPassWord(user);
	    	modelView.addAllObjects(modelMap);  
	    	return modelView;
		}else{
			modelMap.put("code", 0);
    		modelMap.put("result", "验证码错误！");
    		modelView.addAllObjects(modelMap);
    		System.out.println("/p_user/register code error");
    		return modelView;
		}
    }
    
    @RequestMapping("/p_user/modifypassword") 
    @ResponseBody
    public ModelAndView modifypassword(@RequestBody P_User user,HttpServletRequest request){  
    	Map<String,Object> modelMap=new HashMap<String,Object>();
    	ModelAndView modelView=new ModelAndView(); 
    	HttpSession session=request.getSession();
    	String username=(String)session.getAttribute("username");
    	if(username!=null){
    		user.setPay_name(username);
        	modelMap=p_UserService.modifyPassWord(user);
        	modelView.addAllObjects(modelMap);  
        	System.out.println("/p_user/modifypassword");
        	return modelView;
    	}else{
    		modelMap.put("code", 0);
    		modelMap.put("result", "登录失效，请重新登录！");
    		modelView.addAllObjects(modelMap);
    		System.out.println("/p_user/modifypassword no session");
    		return modelView;
    	} 	
    }
    
    @RequestMapping("/p_user/modifyuserinfo") 
    @ResponseBody
    public ModelAndView modifyuserinfo(@RequestBody P_User user,HttpServletRequest request){
    	Map<String,Object> modelMap=new HashMap<String,Object>();
    	
    	ModelAndView modelView=new ModelAndView(); 
    	HttpSession session=request.getSession();
    	String username=(String)session.getAttribute("username");
    	if(username!=null){
        	modelMap=p_UserService.modifyUserInfo(user, username);
        	modelView.addAllObjects(modelMap);
        	System.out.println("/p_user/modifyuserinfo");
        	return modelView;
    	}else{
    		modelMap.put("code", 0);
    		modelMap.put("result", "登录失效，请重新登录！");
    		modelView.addAllObjects(modelMap);
    		System.out.println("/p_user/modifyuserinfo no session");
    		return modelView;
    	}
    }
    
    @RequestMapping("/p_user/checkpaymoney") 
    @ResponseBody
    public ModelAndView checkpaymoney(@RequestBody P_User user,HttpServletRequest request ){
    	Map<String,Object> modelMap=new HashMap<String,Object>();
    	
    	ModelAndView modelView=new ModelAndView(); 
    	HttpSession session=request.getSession();
    	String username=(String)session.getAttribute("username");
    	if(username!=null){
    		user.setPay_name(username);
    		modelMap=p_UserService.checkBankMoney(user); 
        	modelView.addAllObjects(modelMap);
        	System.out.println("/p_user/checkpaymoney");
        	return modelView;
    	}else{
    		modelMap.put("code", 0);
    		modelMap.put("result", "登录失效，请重新登录！");
    		modelView.addAllObjects(modelMap);
    		System.out.println("/p_user/checkpaymoney no session");
    		return modelView;
    	}
    	
    	
    }
    
    @RequestMapping("/p_user/pay") 
    @ResponseBody
    public ModelAndView pay(@RequestBody P_Record pay,HttpServletRequest request ){
    	Map<String,Object> modelMap=new HashMap<String,Object>();

    	ModelAndView modelView=new ModelAndView(); 
    	HttpSession session=request.getSession();
    	String username=(String)session.getAttribute("username");
    	if(username!=null){
    		P_User from=new P_User();
        	P_User to=new P_User();
        	from.setPay_name(username);
        	from.setBank_number(p_UserService.getUserDao().findUserBank(from));
        	from.setPay_password(pay.getPay_password());
        	to.setPay_name(pay.getSeller_name());
        	int tobanknumber=p_UserService.getUserDao().findUserBank(to);
        	System.out.println(tobanknumber);
        	to.setBank_number(tobanknumber);
        	if(pay.getType()==0){
        		modelMap=p_UserService.FromPaytoPay(from, to, pay.getMoney());
        	}else if(pay.getType()==1){
        		modelMap=p_UserService.FromPaytoBank(from, to, pay.getMoney());
        	}else if(pay.getType()==2){
        		modelMap=p_UserService.FromBanktoPay(from, to, pay.getMoney());
        	}else if(pay.getType()==3){
        		modelMap=p_UserService.FromBanktoBank(from, to, pay.getMoney());
        	}else{
        		modelMap.put("result", "fail");
        	}
        	System.out.print("/p_user/pay");
        	modelView.addAllObjects(modelMap);
        	return modelView;
    	}else{
    		modelMap.put("code", 0);
    		modelMap.put("result", "登录失效，请重新登录！");
    		modelView.addAllObjects(modelMap);
    		System.out.print("/p_user/pay no session");
    		return modelView;
    	}
    }
    
    @RequestMapping("/p_user/book_pay") 
    @ResponseBody
    public ModelAndView bookpay(@RequestBody BookPay pay,HttpServletRequest request ){
    	Map<String,Object> modelMap=new HashMap<String,Object>();
    	ModelAndView modelView=new ModelAndView(); 
    	HttpSession session=request.getSession();
//    	String username=(String)session.getAttribute("username");
    		P_User from=new P_User();
        	P_User to=new P_User();
        	System.out.println("/p_user/book_pay");
        	Order order=orderDao.selectOrderById(pay.getOrder_id());
        	
        	int money=order.getTotalprice();
        	int type=pay.getType();
        	P_User buyer=new P_User();
        	System.out.println(pay.getPay_name());
        	System.out.println(pay.getPay_password());
        	buyer.setPay_name(pay.getPay_name());
        	buyer.setPay_password(pay.getPay_password());
        	
        	if(!p_UserService.validate(buyer)){
        		modelMap.put("code", 0);
        		modelMap.put("result","账号密码错误");
        		System.out.println("账号密码错误");
        		modelView.addAllObjects(modelMap);
            	return modelView;
        	}
        	if(type==0){
        		pay.setType(1);
        		from.setPay_name(pay.getPay_name());
            	from.setBank_number(p_UserService.getUserDao().findUserBank(from));
            	System.out.println(from.getBank_number());
            	Bank aBank=new Bank();
            	aBank.setBank_number(from.getBank_number());
            	Bank bank=p_UserService.getBankService().getBankDao().findBankAccountById(aBank);
            	from.setPay_password(bank.getPassword());
        		to.setPay_name("book_trade");
        		int tobanknumber=p_UserService.getUserDao().findUserBank(to);
	        	System.out.println(tobanknumber);
	        	to.setBank_number(tobanknumber);
        	}else if(type==1){
        		pay.setType(0);
        		System.out.println("确认收货");
        		from.setPay_name("book_trade");
        		System.out.println(from.getPay_name());
            	from.setBank_number(p_UserService.getUserDao().findUserBank(from));
            	from.setPay_password("111111");
            	String sellerpayname=p_UserService.getP_userDao().findPaynameByUsername(order.getSeller_name());
            	if(sellerpayname==null){
            		System.out.println("没找到卖家账户");
            	}
            	System.out.println(sellerpayname);
        		to.setPay_name(sellerpayname);
        		int tobanknumber=p_UserService.getUserDao().findUserBank(to);
	        	System.out.println(tobanknumber);
	        	to.setBank_number(tobanknumber);
        	}
        	if(pay.getType()==0){
        		modelMap=p_UserService.FromPaytoPay(from, to, money);
        	}else if(pay.getType()==1){
        		modelMap=p_UserService.FromBanktoPay(from, to, money);
        	}else{
        		modelMap.put("code", 0);
        		modelMap.put("result","支付方式不对");
        	}
        	System.out.print("/p_user/book_pay");
        	modelView.addAllObjects(modelMap);
        	return modelView;
//    	}else{
//    		modelMap.put("code", 0);
//    		modelMap.put("result", "登录失效，请重新登录！");
//    		modelView.addAllObjects(modelMap);
//    		System.out.print("/p_user/pay no session");
//    		return modelView;
//    	}
    }
}
